package com.twxm.breed.iot;

import com.twxm.breed.entity.CategoryInfo;
import com.twxm.breed.entity.DeviceInfo;
import com.twxm.breed.entity.FishPond;
import com.twxm.breed.service.DeviceInfoService;
import com.twxm.breed.service.DissolvedOxygenService;
import com.twxm.breed.service.WaterFactorService;
import com.twxm.breed.service.WeatherFactorService;

import java.util.Date;

public class TimerTask {

    private FishPond fishPond;

    public static WaterFactorService waterFactorService;
    public static WeatherFactorService weatherFactorService;
    public static DissolvedOxygenService dissolvedOxygenService;
    public static DeviceInfoService deviceInfoService;


    // 循环执行的时间间隔
    private long period;

    public TimerTask(FishPond fishPond, long period) {
        this.fishPond = fishPond;
        this.period = period;
    }

    public String getFishPondId(){
        return this.fishPond.getPondId();
    }

    public long getPeriod(){
        return period;
    }

    public void run() {
        System.out.println(getFishPondId() + "runTask" + System.currentTimeMillis() / 1000);
        DeviceInfo oxygen = deviceInfoService.getOxygenSensorByPondId(fishPond.getPondId());
        if(oxygen != null && oxygen.getStatus() == 1){
            CategoryInfo product = oxygen.getCategoryInfo();
            String productKey = product.getProductKey();
            String deviceId = oxygen.getDeviceId();
            runRongJieYang(productKey, deviceId);
        }
        DeviceInfo water = deviceInfoService.getWaterSensorByPondId(fishPond.getPondId());
        if(water != null && water.getStatus() == 1){
            CategoryInfo product = water.getCategoryInfo();
            String productKey = product.getProductKey();
            String deviceId = water.getDeviceId();
            runChuanGanQi(productKey, deviceId);
        }

        DeviceInfo weather = deviceInfoService.getWeatherSensorByPondId(fishPond.getPondId());
        if(weather != null && weather.getStatus() == 1){
            CategoryInfo product = weather.getCategoryInfo();
            String productKey = product.getProductKey();
            String deviceId = weather.getDeviceId();
            runQiXiang(productKey, deviceId);
        }
    }

    public void runChuanGanQi(String productKey, String deviceId){
        System.out.println("runChuanGanQi");
        IOTTask setTask = new IOTTask(waterFactorService);
        try {
            setTask.getWaterFacterDataTask(productKey,deviceId, fishPond.getPondId());
            System.out.println("水质化学传感器定时任务执行了：" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void runQiXiang(String productKey, String deviceId){
        System.out.println("runQiXiang");
        IOTTask setTask = new IOTTask(weatherFactorService);
        try {
            setTask.getWeatherDataTask(productKey,deviceId, fishPond.getPondId());
            System.out.println("气象化学传感器定时任务执行了：" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void runRongJieYang(String productKey, String deviceId){
        System.out.println("runRongJieYang");
        IOTTask setTask = new IOTTask(dissolvedOxygenService);
        try {
            setTask.getOxygenDataTask(productKey,deviceId,fishPond.getPondId());
            System.out.println("水质溶解氧传感器定时任务执行了：" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
